# -*- coding:utf-8 -*-
from selenium import webdriver
import time
import requests
# ========================= 信息-需修改 =========================
username = "zhuyu18"  # 账号
pwd = "*******"  # 密码
recv_qq = "543547055"  # 接收反馈的QQ
loading_time = 5  # 预留页面加载时间5秒 , 视电脑配置修改

# ========================= url-无需修改 =========================
login_url = "https://ehall.jlu.edu.cn/jlu_portal/login"  # 登录url
remind_url = "https://ehall.jlu.edu.cn/taskcenter/workflow/done"  # 登录url

# ========================= xpath-无需修改 =========================
uid_xpath = './/input[@name="username"]'  # 账号xpath
pwd_xpath = './/input[@name="password"]'  # 密码xpath
button_xpath = './/input[@name="login_submit"]'  # 登录按钮xpath
stu_xpath = './/a[@href="/jlu_portal/guide?id=83E36211-C52A-4533-913F-F4D42C677D2A"]'  # 研究生健康打卡按钮xpath
handle_xpath = './/input[@value="我要办理"]'  # 我要办理按钮xpath
loc_con_xpath = './/div[@id="V1_CTRL119_Container"]'  # 所在市容器按钮xpath
loc_xpath = './/div[@class="infoplus_suggester_item suggest_selected"]'  # 所在市下拉框xpath
checkbox_xpath = './/input[@name="fieldCNS"]'  # 承诺按钮xpath
confirm_xpath = './/a[@class="command_button_content"]'  # 确认按钮xpath
ok_xpath = './/button[@class="dialog_button default fr"]'  # 好按钮xpath

# ========================= 驱动-无需修改 =========================
global driver
driver = webdriver.Chrome()  # 设置Chrome驱动


def web_init():
    driver.maximize_window()  # 最大化窗口
    driver.implicitly_wait(6)  # 隐式等待
    driver.get(login_url)  # 获取网页


def web_close():
    driver.quit()  # 关闭所有关联的窗口


def web_load():
    time.sleep(loading_time)  # 预留页面加载时间5秒


def auto_login(username_, pwd_, uid_xpath_, pwd_xpath_, button_xpath_):
    web_load()
    driver.find_element_by_xpath(uid_xpath_).clear()  # 清空已存在内容
    driver.find_element_by_xpath(uid_xpath_).send_keys(username_)  # 传入用户名
    web_load()
    driver.find_element_by_xpath(pwd_xpath_).clear()  # 清空已存在内容
    driver.find_element_by_xpath(pwd_xpath_).send_keys(pwd_)  # 传入密码
    web_load()
    driver.find_element_by_xpath(button_xpath_).click()  # 通过登录按钮并单击登录


def submit(stu_xpath_, handle_xpath_, loc_con_xpath_, loc_xpath_, checkbox_xpath_, confirm_xpath_, ok_xpath_):
    driver.find_element_by_xpath(stu_xpath_).click()  # 研究生健康状况申报
    web_load()
    driver.find_element_by_xpath(handle_xpath_).click()  # 我要办理
    web_load()
    driver.switch_to.window(driver.window_handles[1])  # 切换窗口句柄至最新窗口
    web_load()
    # ========================= 该部分代码针对需要手动选择市区的用户，可以选择注释 =========================
    driver.find_element_by_xpath(loc_con_xpath_).click()   # 定位到复选框容器V1_CTRL119_Container
    web_load()
    driver.find_element_by_xpath(loc_xpath_).click()
    web_load()
    # ========================= 该部分代码针对需要手动选择市区的用户，可以选择注释 =========================
    driver.find_element_by_xpath(checkbox_xpath_).click()  # 本人承诺以上填写内容均真实可靠
    web_load()
    driver.find_element_by_xpath(confirm_xpath_).click()  # 确认填报
    web_load()
    driver.find_element_by_xpath(ok_xpath_).click()  # 好
    web_load()


def remind(remind_url_, recv_qq_):
    driver.get(remind_url_)  # 获取网页
    web_load()
    row = driver.find_elements_by_tag_name('tr')
    result_list = []
    for i in row:
        j = i.find_elements_by_tag_name('td')
        for item in j:
            text = item.text
            result_list.append(text)
    today = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
    result_msg = today+'-'+result_list[0]+'-'+result_list[1]+'-'+result_list[2]+'-'+result_list[3]
    print(result_msg)
    send_private_msg(recv_qq_, result_msg)  # 发送最近的一条打卡记录


def send_private_msg(qq, msg):
    func = "send_private_msg"
    qq_url = "http://localhost:5700/" + func
    params = {
        'user_id': qq,
        'message': msg,
    }
    req = requests.get(qq_url, params)  # 调用酷Q发送消息至接收用户
    print(req.status_code)  # 200-成功


if __name__ == '__main__':
    web_init()  # 驱动初始化
    auto_login(username, pwd, uid_xpath, pwd_xpath, button_xpath)  # 登录操作
    submit(stu_xpath, handle_xpath, loc_con_xpath, loc_xpath, checkbox_xpath, confirm_xpath, ok_xpath)  # 提交操作
    remind(remind_url, recv_qq)  # 发送提醒信息
    web_close()  # 测试结束
